package com.boot.project;

import com.boot.project.domain.CourtDO;
import com.boot.project.domain.CourtTimeDO;
import com.boot.project.service.CourtService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@SpringBootApplication
@EnableScheduling
public class ProjectApplication {
    @Autowired
    private CourtService courtService;
    public static void main(String[] args) {
        SpringApplication.run(ProjectApplication.class, args);
    }
    @Scheduled(cron = "0 59 23 * * ?")
//    @Scheduled(cron = "0 15 0 * * ?")
//    @Scheduled(fixedRate = 1000)
    private void timerTask(){
        courtTask();
    }
    private void courtTask() {
        System.out.println("进来了......................................");
        /** 获取今天的时间 2020-09-16这种格式*/
        Date now = new Date();
        SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd");
        String date = dateformat.format(now);

        /** 从球场表中获取今天的球场信息并且将这些数据塞到7天后*/
        List<CourtDO> courtDOS = courtService.getCourt(date);
        // 获取七天后的日期
        SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd"); //字符bai串转换
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(new Date().getTime());
        c.add(Calendar.DATE, 7);//天后的日期
        Date date4= new Date(c.getTimeInMillis()); //将c转换成Date
//        System.out.println(formatDate.format(date4));
        // 将球场数据塞到7天后
        for (CourtDO i: courtDOS) {
            i.setDateTime(formatDate.format(date4).toString());
            // 修改球场的时间，改为一个星期以后
            Integer updateFlag = courtService.updateCourtDate(i);
            // 修改该球场下面的时间段的状态为1
            Integer updateCourtTimeStatus = courtService.updateCourtTimeStatus(i.getId());
        }
    }
}
